package com.example.demo.mapper;

import com.example.demo.entity.InstrumentCollection;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface InstrumentCollectionMapper {
    // 添加收藏
    @Insert("INSERT INTO instrument_collection_tb (user_id, instrument_id) " +
            "VALUES (#{user_id}, #{instrument_id})")
    int addCollection(InstrumentCollection instrumentCollection);

    // 删除收藏
    @Delete("DELETE FROM instrument_collection_tb WHERE user_id = #{user_id} AND instrument_id = #{instrument_id}")
    int deleteCollection(@Param("user_id") Integer userId, @Param("instrument_id") Integer instrumentId);

    // 检查用户是否收藏某个乐器
    @Select("SELECT COUNT(*) FROM instrument_collection_tb WHERE user_id = #{user_id} AND instrument_id = #{instrument_id}")
    int checkIfCollected(@Param("user_id") Integer userId, @Param("instrument_id") Integer instrumentId);

    @Select("SELECT c.*, i.name AS name, i.type AS type, i.image AS image " +
            "FROM instrument_collection_tb c " +
            "JOIN instrument_tb i ON c.instrument_id = i.instrument_id " +
            "WHERE c.user_id = #{user_id}")
    List<InstrumentCollection> getCollectionsByUserId(@Param("user_id") Integer userId);

}
